package com.ruoyi.training.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.A02ResMateral;
import com.ruoyi.training.service.IA02ResMateralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 材料库Controller
 * 
 * @author WorrilessGo
 * @date 2025-11-18
 */
@RestController
@RequestMapping("/materal")
public class A02ResMateralController extends BaseController
{
    @Autowired
    private IA02ResMateralService a02ResMateralService;

    /**
     * 查询材料库列表
     */
    @RequiresPermissions("training:materal:list")
    @GetMapping("/list")
    public TableDataInfo list(A02ResMateral a02ResMateral)
    {
        startPage();
        List<A02ResMateral> list = a02ResMateralService.selectA02ResMateralList(a02ResMateral);
        return getDataTable(list);
    }

    /**
     * 导出材料库列表
     */
    @RequiresPermissions("training:materal:export")
    @Log(title = "材料库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, A02ResMateral a02ResMateral)
    {
        List<A02ResMateral> list = a02ResMateralService.selectA02ResMateralList(a02ResMateral);
        ExcelUtil<A02ResMateral> util = new ExcelUtil<A02ResMateral>(A02ResMateral.class);
        util.exportExcel(response, list, "材料库数据");
    }

    @PostMapping("/exportModel")
    public void exportModel(HttpServletResponse response)
    {
        List<A02ResMateral> list = new ArrayList<>();
        ExcelUtil<A02ResMateral> util = new ExcelUtil<A02ResMateral>(A02ResMateral.class);
        util.exportExcel(response, list, "材料库数据");
    }

    /**
     * 导入资源库数据
     */
    @RequiresPermissions("training:materal:import")
    @PostMapping("/importData")
    @Transactional
    public AjaxResult importData(MultipartFile file) throws Exception
        {
        ExcelUtil<A02ResMateral> util = new ExcelUtil<A02ResMateral>(A02ResMateral.class);
        List<A02ResMateral> list = util.importExcel(file.getInputStream(),1);
        int result = a02ResMateralService.batchInsertA02ResMateralByMateral(list);
        return toAjax(result);
    }

    /**
     * 获取材料库详细信息
     */
    @RequiresPermissions("training:materal:query")
    @GetMapping(value = "/{materalId}")
    public AjaxResult getInfo(@PathVariable("materalId") String materalId)
    {
        return success(a02ResMateralService.selectA02ResMateralByMateralId(materalId));
    }

    /**
     * 新增材料库
     */
    @RequiresPermissions("training:materal:add")
    @Log(title = "材料库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody A02ResMateral a02ResMateral)
    {
        return toAjax(a02ResMateralService.insertA02ResMateral(a02ResMateral));
    }

    /**
     * 修改材料库
     */
    @RequiresPermissions("training:materal:edit")
    @Log(title = "材料库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody A02ResMateral a02ResMateral)
    {
        return toAjax(a02ResMateralService.updateA02ResMateral(a02ResMateral));
    }

    /**
     * 删除材料库
     */
    @RequiresPermissions("training:materal:remove")
    @Log(title = "材料库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materalIds}")
    public AjaxResult remove(@PathVariable String[] materalIds)
    {
        return toAjax(a02ResMateralService.deleteA02ResMateralByMateralIds(materalIds));
    }
}
